package com.zlp.clazz;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
* 测试动态代理生成字节码技术
* */
public class DynamicProxyDemo_1 {

    interface IHello{
        void hello();
    }
    static class Hello implements IHello{

        @Override
        public void hello() {
            System.out.println("hello.....");
        }
    }

    static class DynamicProxy<T> implements InvocationHandler {
        T t;
        public DynamicProxy(T t){
            this.t = t;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("welcome.......");
            return method.invoke(t,args);
        }

        public Object getProxy(){
            return Proxy.newProxyInstance(t.getClass().getClassLoader(),t.getClass().getInterfaces(),this);
        }
    }

    public static void main(String[] args) {

        IHello proxy = (IHello)new DynamicProxy<IHello>(new Hello()).getProxy();
        System.getProperties().put("sun.miscProxyGenerator.saveGeneratedFiles","true");
        proxy.hello();

        System.out.println(proxy.toString());

        System.out.println(proxy.hashCode());
    }
}
